/*******************************************************************************
 * Copyright (c) Jonathan Buck - jonbuck16@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    jonbuck16@gmail.com - initial API and implementation
 *******************************************************************************/
package net.jonbuck.tassoo.ui.validation;

import net.jonbuck.tassoo.ui.nl.Messages;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.validation.MultiValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;

public class MakeDefaultValidator extends MultiValidator {

	private IObservableValue makeDefaultButton;

	private IObservableValue newDefaultValueCombo;

	private boolean isDefaultItem;

	private boolean isEdit;

	private String type;

	/**
	 * 
	 * @param makeDefaultButton
	 * @param newDefaultValueCombo
	 * @param isDefaultItem
	 * @param isEdit
	 * @param type
	 */
	public MakeDefaultValidator(IObservableValue makeDefaultButton, IObservableValue newDefaultValueCombo, boolean isDefaultItem,
			boolean isEdit, String type) {
		super();
		this.makeDefaultButton = makeDefaultButton;
		this.newDefaultValueCombo = newDefaultValueCombo;
		this.isDefaultItem = isDefaultItem;
		this.isEdit = isEdit;
		this.type = type;
	}

	/**
	 * 
	 */
	protected IStatus validate() {
		Boolean makeDefault = (Boolean) makeDefaultButton.getValue();
		String newDefaultValue = (String) newDefaultValueCombo.getValue();

		if (isEdit && isDefaultItem) {
			if (makeDefault == false && StringUtils.isBlank(newDefaultValue)) {
				return ValidationStatus.error(Messages.bind(Messages.editreferencemodeldialog_error_defaultcategory,
						StringUtils.lowerCase(type)));
			}
		}

		return ValidationStatus.ok();
	}

}
